package com.cx.update;

import com.cx.widget.CleverDialog;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;

public class UpdateTask 
{
	private final static String HAS_UPDATE_APK_ACTION="com.cx.update_apk";
	public final static int MSG_ORANGETV_NEED_INSTALL = 1;
	public final static int MSG_ORANGETV_DOWNLOAD_ERROR = 2;
	private UpdateUtils mUpdateUtils=null;
	private Activity ctx;
	private String base_url;
	private IntentFilter filter=null;
	
	 public static String downloadAddress = "";
	 public static String downloadMd5 = "";
	 public static String[] updateMsgs;
	 
	 private CleverDialog mDialog=null;
	    
	public UpdateTask(Activity ctx, String base_url)
	{
		this.ctx = ctx;
		this.base_url = base_url;
		mDialog = new CleverDialog(ctx, handler, ctx.getWindow().getDecorView());
	}
	public void start()
	{
		if(filter==null)regist();
		 if(null == mUpdateUtils) {
	            mUpdateUtils = new UpdateUtils();
	            mUpdateUtils.init(ctx);
	        }
		 infoThread.start();
	}
	
	public void stop()
	{
		if(mDialog!=null)  mDialog.close();
		if(null !=filter)ctx.unregisterReceiver(loadReceiver);
		filter=null;
	}
	private void regist(){
	  filter = new IntentFilter();
     filter.addAction(HAS_UPDATE_APK_ACTION);
     ctx.registerReceiver(loadReceiver, filter);
	}

	private Thread infoThread = new Thread(new Runnable(){

		@Override
		public void run() {
			
			int value = mUpdateUtils.setUpdateAddress(base_url);
			
	        if (value == UpdateUtils.UPDATE_NEED) {
	            downloadAddress = mUpdateUtils.getUpdateInfo().getUrl();
	            downloadMd5 = mUpdateUtils.getUpdateInfo().getMd5();
	            updateMsgs = mUpdateUtils.getMessage();
	            // intent.putExtra("apk_path", path);
	            Intent intent = new Intent(HAS_UPDATE_APK_ACTION);
	            ctx.sendBroadcast(intent);
	        } 
		}});
	
	private BroadcastReceiver loadReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			 String action = intent.getAction();
			 System.out.println("===BroadcastReceiver ===="+action);
			if(HAS_UPDATE_APK_ACTION.equals(action))
			{
				 mDialog.dismiss();
				 mDialog.showDownLoading(null, null, new OnClickListener() {
                     @Override
                     public void onClick(View v) {
                         mDialog.dismiss();                         
                     }
                 }, null);
			}
		}
		
	};
	
	 private Handler handler = new Handler() {
	        @Override
	        public void handleMessage(Message msg) {
	           
	            switch (msg.what) {
	                
	                case MSG_ORANGETV_NEED_INSTALL:
	                    String path = (String) msg.obj;
	                    if (!TextUtils.isEmpty(path)) {
	                        try {
	                            FileUtils.startInstallApp(ctx, path);
	                            return;
	                        } catch (Exception e) {
	                            e.printStackTrace();
	                        }
	                    }
	                    //ToastHelper.fastToast(WelcomLiveTV.this, getString(R.string.download_failed));
	                    //finish();
	                    break;
	                case MSG_ORANGETV_DOWNLOAD_ERROR:
	                    String errorMsg = (String) msg.obj;
	                    if (null != mDialog && !TextUtils.isEmpty(errorMsg)) {
	                        mDialog.setTitle(errorMsg);
	                        mDialog.changeBtn(CleverDialog.BTN_INDEX_LEFT,
	                                ctx.getString(R.string.check_again), new OnClickListener() {
	                                    @Override
	                                    public void onClick(View v) {

	                                        mDialog.dismiss();
	                                    }
	                                });
	                    }
//	                    else {
//	                        ToastHelper.fastToast(WelcomLiveTV.this,
//	                                getString(R.string.download_failed));
//	                        finish();
//	                    }
	                    break;
	                default:
	                    break;
	            }
	        }
	    };
}
